#include "history.h"

History::History ()
{}

// ****

History::History (char *fnamec):
    fname(fnamec)
{

    ifstream scin(fname);

	scin >> npoints;
	tim = new double[npoints];
	val = new double[npoints];
	for(int i=0; i<npoints; i++)
	{ 
		scin >>  tim[i] >> val[i];
		//cout << "tim[" << i << "]=" << tim[i];
		//cout << "val[" << i << "]=" << val[i];
	}				
   n0=1;		
}

// ****

History::History (double value)
{
	npoints = 2;
	tim = new double[npoints];
	val = new double[npoints];
	tim[0] = 0.0;
	tim[1] = 1.0e30;
	val[0] = value;
	val[1] = value;
}

// ****

History::~History() {}

// ****

int History::hunt(double& talpha) {

  int ju, jm, jl;

   jl=0;
   ju=npoints;
   while(ju-jl > 1) {
      jm = (ju+jl) >> 1;
      if(talpha > tim[jm])
         jl=jm;
      else
         ju=jm;
   }
   if (jl!=npoints-1) {
      n0=jl;
      return(1);
   } else if(talpha==tim[npoints-1]) return(1);
   else return(0);
}

// ****

double History::value(double& talpha)
{
	/*
	cout << "talpha = " << talpha << endl;
	cout << "n0 = " << n0 << endl;
	cout << "tim[n0] = " << tim[n0] << endl;
	cout << "val[n0+1] = " << val[n0+1] << endl;
	cout << "val[n0] = " << val[n0] << endl;
	cout << "tim[n0+1] = " << tim[n0+1] << endl;
	*/
	if (hunt(talpha)==1)	return ((talpha-tim[n0])*(val[n0+1]-val[n0])/(tim[n0+1]-tim[n0]) + val[n0]) ;
//	else return 0.0;
	else return -9999.9999;
}

// ****

/*
double History::valueplusone(double& tplusone) {
// Devuelve el valor no en talpha, sino en (n+1)
// que son distintos, por ejemplo, en la regla del punto medio.
	
  return ((tplusone-tim[n0])*(val[n0+1]-val[n0])/(tim[n0+1]-tim[n0]) + val[n0]) ;
}
*/
/*
void History::print() { cout << "In history\n"; }
*/